package com.zb.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author WEN
 * @Date 2023/7/18 0018
 **/
@Data
@ApiModel(value = "统一返回结果类封装")
public class Result {
    @ApiModelProperty(value = "状态码，20000表示成功")
    private long code;

    @ApiModelProperty(value = "是否成功，true表示成功，false-表示失败")
    private boolean success;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据对象")
    private Map<String, Object> data = new HashMap<>();

    public Result data(String k, Object v) {
        this.data.put(k, v);
        return this;
    }

    public Result data(Map<String, Object> data) {
        this.data = data;
        return this;
    }

    private Result(long code, boolean success, String message) {
        this.code = code;
        this.success = success;
        this.message = message;
    }

    public static Result success() {
        return new Result(ResultCode.SUCCESS.getCode(), true, ResultCode.SUCCESS.getMessage());
    }

    public static Result success(String message) {
        return new Result(ResultCode.SUCCESS.getCode(), true, StringUtils.isNotBlank(message)? message : ResultCode.SUCCESS.getMessage());
    }

    public static Result error(long code, String message) {
        return new Result(code, false, message);
    }
}
